package com.CST.service.impl.ys;

import com.CST.common.BaseEntity;
import com.CST.dto.ys.YsDepartmentControlDTO;
import com.CST.entity.ys.YsDepartment;
import com.CST.entity.ys.YsDepartmentControl;
import com.CST.mapper.cst.ys.YsDepartmentControlMapper;
import com.CST.service.ys.YsDepartmentControlService;
import com.CST.service.ys.YsDepartmentService;
import com.CST.util.ExcelTransfer;
import com.CST.vo.ys.YsDepartmentControlVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author nmx
 * @since 2023-06-21
 */
@Service
public class YsDepartmentControlServiceImpl extends ServiceImpl<YsDepartmentControlMapper, YsDepartmentControl> implements YsDepartmentControlService {

    private final ExcelTransfer<YsDepartmentControlVO> excelTransfer;
    private final ExcelTransfer<YsDepartmentControl> excelTransfer1;

    private final YsDepartmentService ysDepartmentService;

    public YsDepartmentControlServiceImpl(ExcelTransfer<YsDepartmentControlVO> excelTransfer, ExcelTransfer<YsDepartmentControl> excelTransfer1, YsDepartmentService ysDepartmentService) {
        this.excelTransfer = excelTransfer;
        this.excelTransfer1 = excelTransfer1;
        this.ysDepartmentService = ysDepartmentService;
    }

    @Override
    public IPage<YsDepartmentControlVO> listYsDepartmentControlPage(YsDepartmentControlDTO dto) {
        return baseMapper.selectPageNew(dto.getPage(), Wrappers.lambdaQuery(YsDepartmentControl.class)
                .eq(StringUtils.isNotBlank(dto.getDeptId()), YsDepartmentControl::getDepartmentId, dto.getDeptId())
                .in(dto.getIds() != null && !dto.getIds()
                        .isEmpty(), BaseEntity::getId, dto.getIds())
                .like(StringUtils.isNotBlank(dto.getYyName()), YsDepartmentControl::getYyDepartmentName,
                        dto.getYyName()));
    }

    @Override
    public void importExcelYsDepartmentControl(MultipartFile file) throws ClassNotFoundException {
        List<YsDepartment> ysDepartments = ysDepartmentService.list(Wrappers.lambdaQuery(YsDepartment.class)
                .eq(BaseEntity::getDeleteFlag, Boolean.FALSE));
        Map<String, Long> map = ysDepartments.stream()
                .collect(Collectors.toMap(YsDepartment::getDepartmentCode, BaseEntity::getId));
        excelTransfer1.importExcel(file, this, list -> {
            List<YsDepartmentControl> arrayList = new ArrayList<>();
            list.forEach(e -> {
                YsDepartmentControl control = new YsDepartmentControl();
                BeanUtils.copyProperties(e, control);
                if (map.containsKey(e.getDepartmentCode())) {
                    control.setDepartmentId(Math.toIntExact(map.get(e.getDepartmentCode())));
                    arrayList.add(control);
                }
            });
            return arrayList;
        });
    }

    @Override
    public void exportExcelYsDepartmentControl(HttpServletResponse response, YsDepartmentControlDTO dto) {
        excelTransfer.exportExcel(response, listYsDepartmentControlPage(dto).getRecords(),
                "部门对照", "sheet", YsDepartmentControlVO.class);
    }

    @Override
    public void exportExcelYsDepartmentControlDemo(HttpServletResponse response) {
        excelTransfer.exportExcel(response, new ArrayList<>(),
                "部门对照", "sheet", YsDepartmentControlVO.class);
    }
}
